using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

namespace VTGGoldMiner
{
    //lop duoc tinh toan theo chuan man hinh 640x480 lien ket voi lop NhanVat, hien thi co dan theo ti le
    //dau vao la cac hinh anh chuan chua duoc co dan.
    class VatThe
    {
        private DaoVang trochoi;
        public string tenvatthe, theloai;
        public int toadox, toadoy, chieurong, chieudai, anhien, giatri, gocxoay = 0;//1 la hien thi, 2 la an
        public Vector2 tamxoay, diembigap;
        public float donang;
        public Texture2D anhhienthi;
        private float tiledanrong, tiledandai;
        private CacHam dungham;
        private Texture2D[] anhthanhphan;
        public int soanhthanhphan = 1;
        private int chayhoatcanh = 0;
        public int huongdichuyen = 0;//bang 0 la chay phai, bang soanhthanhphan la chay trai
        public int tocdodichuyen = 3, tocdoht = 3;//toc do tu 1 den 7
        private int demhoatcanh = 0, sodemhoatcanh = 0;
        public int tinhtrang = 0;//tinh trang 0 la chua bi gap len
        private Vector2 vungtocdo = new Vector2(1, 7);//vung toc do ngau nhien tu 1 den 7
        private int kieudongtinh = 0;//neu bang 0 thi vat the tinh khong di chuyen, bang 1 thi dich chuyen theo toc do
        public VatThe(DaoVang trochoichinh, Texture2D[] anh, int dongtinh, Vector2 vtocdo, string ten, string loai, Rectangle bovatthe, int ah, int gia, float nang, Vector2 diemgap, float tilerong, float tiledai)
        {
            trochoi = trochoichinh;
            dungham = new CacHam(trochoi.GraphicsDevice);
            tenvatthe = ten;
            theloai = loai;
            anhthanhphan = anh;
            soanhthanhphan = Convert.ToInt32(anhthanhphan.Length / 2);
            anhhienthi = anhthanhphan[0];
            kieudongtinh = dongtinh;
            vungtocdo = vtocdo;
            toadox = bovatthe.X;
            toadoy = bovatthe.Y;
            chieurong = bovatthe.Width;
            chieudai = bovatthe.Height;
            anhien = ah;
            giatri = gia;
            donang = nang;
            diembigap = diemgap;
            tiledanrong = tilerong;
            tiledandai = tiledai;
        }
        public void HoatCanh()
        {
            if (kieudongtinh == 1)
            {
                if (demhoatcanh == sodemhoatcanh)
                {
                    demhoatcanh = 0;
                    chayhoatcanh = 0;
                    int huong = Convert.ToInt32(trochoi.ngaunhien.Next(1, 3));
                    if (huong == 1) huongdichuyen = 0;
                    else huongdichuyen = soanhthanhphan;
                    tocdodichuyen = Convert.ToInt32(trochoi.ngaunhien.Next(Convert.ToInt32(vungtocdo.X), Convert.ToInt32(vungtocdo.Y) + 1));
                    tocdoht = 8 - tocdodichuyen;
                    sodemhoatcanh = Convert.ToInt32(trochoi.ngaunhien.Next(20, 70));
                }
                demhoatcanh++;
                if (tinhtrang == 0)
                {
                    if (huongdichuyen == 0)
                    {
                        if (toadox < 640 - chieurong - tocdodichuyen) toadox += tocdodichuyen;
                        else huongdichuyen = soanhthanhphan;
                    }
                    else
                    {
                        if (toadox > chieurong + tocdodichuyen) toadox -= tocdodichuyen;
                        else huongdichuyen = 0;
                    }
                }
            }
            chayhoatcanh++;
            for (int i = 0; i < soanhthanhphan; i++)
            {
                if (chayhoatcanh == (i + 1) * tocdoht)
                {
                    if ((i + 1) == soanhthanhphan)
                    {
                        anhhienthi = anhthanhphan[0 + huongdichuyen];
                        chayhoatcanh = 0;
                    }
                    else anhhienthi = anhthanhphan[(i + 1) + huongdichuyen];
                }
            }
        }
        public void DichChuyen(int tdx, int tdy)
        {
            toadox = tdx;
            toadoy = tdy;
        }
        public Rectangle LanhThoVatThe()
        {
            return new Rectangle(toadox, toadoy, chieurong, chieudai);
        }
        public void HienThi(SpriteBatch nenve)
        {
            if ((anhien == 1) & (tenvatthe != "rong"))
                dungham.XoayHinhAnh(nenve, anhhienthi, gocxoay, dungham.BoHinhAnh(toadox, toadoy, chieurong, chieudai, tiledanrong, tiledandai), tamxoay, tiledanrong, tiledandai);
        }
    }
}